package com.greenline.push.core.pushcenter.impl;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.greenline.push.core.dto.DiseaseDto;
import com.greenline.push.core.pushcenter.DiseaseManager;

public class DiseaseManagerImpl implements DiseaseManager {

	@Override
	public List<DiseaseDto> getDiseaseIdByKeyword(String keyword) {

		CloseableHttpClient httpclient = HttpClients.createDefault();
		String url = "http://192.168.1.38:2000/keyword_analysis?q=" + keyword;
		String responseBody = "";
		try {
			HttpGet httpget = new HttpGet(url);

			System.out.println("Executing request " + httpget.getRequestLine());

			// Create a custom response handler
			ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

				public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
					int status = response.getStatusLine().getStatusCode();
					if (status >= 200 && status < 300) {
						HttpEntity entity = response.getEntity();
						return entity != null ? EntityUtils.toString(entity) : null;
					} else {
						throw new ClientProtocolException("Unexpected response status: " + status);
					}
				}

			};
			
			try {
				responseBody = httpclient.execute(httpget, responseHandler);
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			System.out.println("----------------------------------------");
			System.out.println(responseBody);
		} finally {
			try {
				httpclient.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		List<DiseaseDto> diseaseList = JSON.parseArray(responseBody, DiseaseDto.class);
		return diseaseList;
	}

	public static void main(String[] args) {
		String keyword = "红眼病";

		CloseableHttpClient httpclient = HttpClients.createDefault();
		String url = "http://192.168.1.38:2000/keyword_analysis?q=" + keyword;
		try {
			HttpGet httpget = new HttpGet(url);

			System.out.println("Executing request " + httpget.getRequestLine());

			// Create a custom response handler
			ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

				public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
					int status = response.getStatusLine().getStatusCode();
					if (status >= 200 && status < 300) {
						HttpEntity entity = response.getEntity();
						return entity != null ? EntityUtils.toString(entity) : null;
					} else {
						throw new ClientProtocolException("Unexpected response status: " + status);
					}
				}

			};
			String responseBody = "";
			try {
				responseBody = httpclient.execute(httpget, responseHandler);
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			List<DiseaseDto> diseaseList = JSON.parseArray(responseBody, DiseaseDto.class);
			if (diseaseList != null) {
				for(DiseaseDto disease : diseaseList){
					String word = disease.getWord();
					String uuid = disease.getUuid();
					String type = disease.getType();
					System.out.println(word);
					System.out.println(uuid);
					System.out.println(type);
				}
			}
			System.out.println("----------------------------------------");
			System.out.println(responseBody);
		} finally {
			try {
				httpclient.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}
